package com.pa165.languageschoolpersistence.DAOImpl;

import com.pa165.languageschoolpersistence.DAO.LectorDAO;
import com.pa165.languageschoolpersistence.Exceptions.IllegalArgumentPersistenceException;
import com.pa165.languageschoolpersistence.Exceptions.ServiceFailureException;
import com.pa165.languageschoolpersistence.entities.Lector;
import com.pa165.languageschoolpersistence.entities.Lecture;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Tomas Hruby
 */
@Repository("lectorDAO")
public class LectorDAOImpl implements LectorDAO {

    private EntityManager em;

    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    public Lector add(Lector lector) throws ServiceFailureException {
        if (lector == null) {
            throw new IllegalArgumentPersistenceException("Lector is null");
        }
        if (lector.getId() != null) {
            return null;
        }
        if (lector.getName() == null) {
            throw new IllegalArgumentPersistenceException("Lector name is null");
        }
        if (lector.getLectures() == null) {
            throw new IllegalArgumentPersistenceException("List of lectures is null");
        }
        if (lector.getLevelofLanguage() == null) {
            throw new IllegalArgumentPersistenceException("Level of language is null");
        }
        em.persist(lector);
        return lector;
    }

    public void remove(Lector lector) throws IllegalArgumentPersistenceException {
        if (lector == null) {
            throw new IllegalArgumentPersistenceException("Lector is null");
        }
        if (lector.getId() == null) {
            throw new IllegalArgumentPersistenceException("Lector.id is null");
        };
        if (lector.getLectures() == null) {
            throw new IllegalArgumentPersistenceException("List of lectures is null");
        }
        if (lector.getLevelofLanguage() == null) {
            throw new IllegalArgumentPersistenceException("Level of language is null");
        }

        Lector lectorToRemove = em.find(Lector.class, lector.getId());
        em.remove(lectorToRemove);

    }

    public Lector modify(Lector lector) throws IllegalArgumentPersistenceException {
        if (lector == null) {
            throw new IllegalArgumentPersistenceException("Lector is null");
        }
        if (lector.getId() == null) {
            throw new IllegalArgumentPersistenceException("Lector.id is null");
        };
        if (lector.getName() == null) {
            throw new IllegalArgumentPersistenceException("Lector name is null");
        }
        if (lector.getLectures() == null) {
            throw new IllegalArgumentPersistenceException("List of lectures is null");
        }
        if (lector.getLevelofLanguage() == null) {
            throw new IllegalArgumentPersistenceException("Level of language is null");
        }

        em.merge(lector);
        return lector;
    }

    public Lector get(Long id) throws IllegalArgumentPersistenceException {
        if (id == null) {
            throw new IllegalArgumentPersistenceException("Id is null");
        }

        Lector result;
        result = em.find(Lector.class, id);
        return result;
    }

    public List<Lector> getAll() {
        Query q;
        List<Lector> result = null;
        result = em.createQuery("SELECT s FROM Lector s").getResultList();
        return result;
    }

    public Lecture enrollLecture(Lecture lecture, Lector lector) throws IllegalArgumentPersistenceException {
        if (lector == null) {
            throw new IllegalArgumentPersistenceException("Lector is null");
        }
        if (lector.getId() == null) {
            throw new IllegalArgumentPersistenceException("Lector.id is null");
        };
        if (lector.getName() == null) {
            throw new IllegalArgumentPersistenceException("Lector name is null");
        }
        if (lector.getLectures() == null) {
            throw new IllegalArgumentPersistenceException("List of lectures is null");
        }
        if (lector.getLevelofLanguage() == null) {
            throw new IllegalArgumentPersistenceException("Level of language is null");
        }

        if (lecture == null) {
            throw new IllegalArgumentPersistenceException("Lecture is null");
        }

        List<Lecture> lecturesTmp = lector.getLectures();
        if (lecturesTmp.contains(lecture)) {
            return null;
        }
        lector.addLecture(lecture);
        lecture.setLector(lector);
        em.merge(lector);
        em.merge(lecture);
        return lecture;
    }

    public List<Lector> findByName(String name) throws IllegalArgumentPersistenceException {
        if (name == null) {
            throw new IllegalArgumentPersistenceException("Name is null");
        }
        List<Lector> result = null;
        Query q;
        result = em.createQuery("SELECT s FROM Lector s WHERE s.name=:name")
                .setParameter("name", name).getResultList();
        return result;

    }
}
